package week4;

import java.util.concurrent.*;

public class Q1 {
    static Callable<Integer> c = new Callable() {
        @Override
        public Integer call() throws Exception {
            return fib();
        }

        int fib() {
            int[] fibs = new int[200];
            fibs[1] = 1;
            for (int i = 2; i < fibs.length; i++) {
                fibs[i] = fibs[i - 1] + fibs[i - 2];
            }
            return fibs[fibs.length - 1];
        }
    };

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        run1();
    }

    public static void run1() throws ExecutionException, InterruptedException {

        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        Future f = threadPool.submit(c);
        System.out.println("The return from child thread is: " + f.get());
        threadPool.shutdown();
    }

    public static void run2() throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask = new FutureTask(c);
        futureTask.run();
        System.out.println("The return from child thraed is: " + futureTask.get());
    }
}
